package com.zwb.mongostudy.config;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.lang.NonNull;

import java.util.*;

/**
 * @author zhongwb
 */
public class DefaultMongoTemplateManager implements MongoTemplateManager {
    private final MongoTemplate defaultTemplate;
    private Map<String, MongoTemplate> templateMap = new HashMap<>();

    public DefaultMongoTemplateManager(MongoTemplate defaultTemplate, Map<String, MongoTemplate> templateMap) {
        this.defaultTemplate = defaultTemplate;
        if (templateMap != null) {
            this.templateMap = templateMap;
        }
    }

    @Override
    public Optional<MongoTemplate> getTemplate(@NonNull String datasourceName) {
        return Optional.ofNullable(this.templateMap.get(datasourceName));
    }

    @Override
    public Optional<MongoTemplate> getDefaultTemplate() {
        return Optional.ofNullable(this.defaultTemplate);
    }


    @Override
    public Collection<MongoTemplate> getAllTemplates() {
        return Collections.unmodifiableCollection(this.templateMap.values());
    }


}
